package com.potenza.training.reply.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.potenza.training.reply.dao.SubjectDao;
import com.potenza.training.reply.dao.SubjectReplyDao;
import com.potenza.training.reply.entity.SubjectEntity;
import com.potenza.training.reply.entity.SubjectReplyEntity;
import com.potenza.training.reply.service.SubjectReplyService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;


@Service("subjectReplyService")
public class SubjectReplyServiceImpl extends ServiceImpl<SubjectReplyDao, SubjectReplyEntity> implements SubjectReplyService {

    @Resource
    SubjectReplyDao subjectReplyDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SubjectReplyEntity> page = this.page(
                new Query<SubjectReplyEntity>().getPage(params),
                new QueryWrapper<SubjectReplyEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    @Transactional
    public void insert(int id, int[] ids) {
        SubjectReplyEntity subjectReplyEntity = new SubjectReplyEntity();
        for (int i : ids) {
            subjectReplyEntity.setTbReplyGroupId(new Long(id));
            subjectReplyEntity.setTbSujectId(new Long(i));
            subjectReplyDao.insert(subjectReplyEntity);
        }
    }

    @Resource
    private SubjectDao subjectDao;

    @Override
    public List<SubjectEntity> findByGroupId(Long gid) {
        QueryWrapper<SubjectReplyEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(SubjectReplyEntity::getTbReplyGroupId,gid);

        //中间表数据
        List<SubjectReplyEntity> subjectReplyEntities = subjectReplyDao.selectList(queryWrapper);
        List<SubjectEntity> list = new ArrayList<>();

        for (SubjectReplyEntity subjectReplyEntity : subjectReplyEntities) {
            list.add(subjectDao.selectById(subjectReplyEntity.getTbSujectId()));
        }

        return list;
    }


}